package machines;

public class MoneyChangeMachine {
    protected static final String PASSWORD = "abc";
    protected static boolean isOwner = false;
    protected static double inputValue;
    protected static int [] notes;
    protected static int [] coins;
    protected static int [] changeCoins;
    private static final String MENU = "\n*******************************"
                                        + "\nWillkommen zur Bank uns"
                                        + "\nBitte Betriebsmodus wählen:"
                                        + "\n1. Geld wechseln"
                                        + "\n2. Geld nachfüllen"
                                        + "\n3. Get available coins"
                                        + "\n4. Beenden."
                                        + "\n5. Administration."
                                        + "\n****************************";
    public static String giveChange() {
        return null;
    }
    
    public static void authenticate(String s) throws Exception {
        if (s.equals(PASSWORD)) {
            isOwner = true;           
        } else {
            isOwner = false;
            throw new Exception("Password is invalid!!!");
        }
    }
    
    public static String getMenu() {
        return MENU;
    }
    
    public static String getOutputCoins(){
        StringBuilder sb = new StringBuilder();
        double faceValue = 0;         
        double line = 0;
        
        for (int i = 0; i < changeCoins.length ; i++){
            switch(i) {
                case 0: faceValue = 2;
                   break;
                case 1: faceValue = 1;
                    break;
                case 2: faceValue = 0.5;
                    break;
                case 3: faceValue = 0.2;
                    break;
                case 4: faceValue = 0.1;
                    break;
                case 5: faceValue = 0.05;
                    break;
            }
            line = changeCoins[i] * faceValue; 
            sb.append("\n Münze: ").append(faceValue).append(", Anzahl: ").append(changeCoins[i]);                     
        } 
        return sb.toString();
    }

    public static String getAvailableCoins(){
        StringBuilder sb = new StringBuilder();
        double faceValue = 0;
        double totalValue = 0;
        double line;
        
        for (int i = 0 ;i < coins.length-1 ; i++){
            switch(i){
                case 0: faceValue = 2;
                   break;
                case 1: faceValue = 1;
                    break;
                case 2: faceValue = 0.5;
                    break;
                case 3: faceValue = 0.2;
                    break;
                case 4: faceValue = 0.1;
                    break;
                case 5: faceValue = 0.05;
                    break;
                default: break;
                    
                
            }
                line = coins[i]*faceValue; 
                totalValue += line;
             sb.append("\n Münze: ").append(faceValue).append(", Anzahl: ").append(coins[i]).append(", Wert : ").append(line); 
        }
        sb.append("\n" + "Gesamtwert: ").append(totalValue);  
        
        return sb.toString();
    }

    
    
    public static void loadCoins(Coin type, int quantity) throws Exception {
        if(isOwner = true) {     
            switch (type) {
                case EUR_2:
                    coins [0] += quantity;
                    break;
                case EUR_1:
                    coins [1]+= quantity;
                    break;
                case CENT_50:
                    coins [2] += quantity;
                    break;
                case CENT_20:
                    coins [3] += quantity;
                    break;
                case CENT_10:
                    coins [4] += quantity;
                    break;
                case CENT_5:
                    coins[5] += quantity;
                    break;
            }
        }
        else {
            throw new Exception("User is not authenticated!!!");
        }
            
                
        
    }
    public static void loadNotes(Note type, int quantity) {
        switch (type) {
            case EUR_500:
                notes [0] += quantity;
                inputValue += 500*quantity;
                break;
            case EUR_200:
                notes [1]+= quantity;
                inputValue += 200*quantity;
                break;
            case EUR_100:
                notes [2] += quantity;
                break;
            case EUR_50:
                notes [3] += quantity;
                inputValue += 50*quantity;
                break;
            case EUR_20:
                notes [4] += quantity;
                inputValue += 20*quantity;
                break;
            case EUR_10:
               notes[5] += quantity;
               inputValue += 10*quantity;
                break;
            case EUR_5:
               notes[6] += quantity;
               inputValue += 5*quantity;
                break;     
        }
        
        
    }
    
    public static void start() {
        coins = new int[7];        
        notes = new int[7];       
        changeCoins = new int [6];
    }
    
    public static String change() {
        double changeValue = 0;
            
        changeCoins[0] = 0;
        changeCoins[1] = 0;
        changeCoins[2]= 0;
        changeCoins[3]= 0;
        changeCoins[4]= 0;
        changeCoins[5]= 0;
            
        // Bestand checken
        changeValue = coins[0] * 2+ coins[1] * 1 + coins[2] * 0.5 + coins[3] * 0.2 + coins[4] * 0.1 + coins[5] * 0.05;

        if (changeValue >= inputValue) {

            // 2€ ausgeben
            while (inputValue >= 2 && coins[0] > 0){
                inputValue -= 2;
                coins[0] -= 1;
                changeCoins[0]++; // += 1; 
            }
            // 1€ ausgeben
            while (inputValue >= 1 && coins[1] > 0){
               inputValue -= 1;
               coins[1] -= 1;
               changeCoins[1] += 1; 
            }
            // 0,5€ ausgeben
            while (inputValue >= 0.5 && coins[2] > 0){
               inputValue -= 0.5;
               coins[2] -= 1;
               changeCoins[2] += 1; 
            }
            // 0,20€ ausgeben
            while (inputValue >= 0.2 && coins[3] > 0){
               inputValue -= 0.2;
               coins[3] -= 1;
               changeCoins[3] += 1; 
            }
            // 0,10€ ausgeben
            while (inputValue >= 0.1 && coins[4] > 0){
               inputValue -= 0.1;
               coins[4] -= 1;
               changeCoins[4] += 1; 
            }
            // 0.05€ ausgeben
            while (inputValue >= 2 && coins[5] > 0){
               inputValue -= 0.05;
               coins[5] -= 1;
               changeCoins[5] += 1;
            }
            
            return "\n*******************************"
                    + "\n2€ Münzen: "+ changeCoins[0]
                    + "\n1€ Münzen: "+ changeCoins[1]
                    + "\n50ct Münzen: "+ changeCoins[2]
                    + "\n20ct Münzen: "+ changeCoins[3]
                    + "\n10ct Münzen: "+ changeCoins[4]
                    + "\n5ct Münzen: "+ changeCoins[5]
                    + "\n*******************************";
        } else {
            String msg = "\n*******************************"
                    + "\nNicht genug Geld. Hier ihre Eingaben:"
                    + "\n500€ Schein: "+ notes[0]
                    + "\n200€ Schein: "+ notes[1]
                    + "\n100€ Schein: "+ notes[2]
                    + "\n50€ Schein: "+ notes[3]
                    + "\n20€ Schein: "+ notes[4]
                    + "\n10€ Schein: "+ notes[5]
                    + "\n5€ Schein: "+ notes[6]
                    + "\n*******************************";
            
            for (int i = 0; i < notes.length; i ++) {
                notes[i] = 0;
            }
            
            return msg;
            
        }
        
        
    } 
}